package com.dinadi.yidi.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dinadi.yidi.R;
import com.dinadi.yidi.bean.MyCollectionRequestEntity;
import com.dinadi.yidi.ui.activity.CreativeHotspotActivity;
import com.dinadi.yidi.ui.activity.CreativeHotspotDetailsActivity;
import com.dinadi.yidi.ui.activity.ProductDetailsActivity;
import com.dinadi.yidi.utils.ImageUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * author: JiaYu.He
 * Date: 2017/3/22
 * Time: 13:50
 */

public class MyCollectionAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private Context mContext;
    private List<MyCollectionRequestEntity> mDatas = new ArrayList<>();
    private String type, productId;

    public void addAllData(List<MyCollectionRequestEntity> dataList) {
        this.mDatas.addAll(dataList);
        notifyDataSetChanged();
    }

    public void clearData() {
        this.mDatas.clear();
    }

    public MyCollectionAdapter(Context context) {
        mContext = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_my_collection_item, parent, false);
        return new MyCollectionAdapter.ItemViewHolder(v);

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        MyCollectionAdapter.ItemViewHolder itemViewHolder = (MyCollectionAdapter.ItemViewHolder) holder;
        itemViewHolder.text_msg.setText(mDatas.get(position).getTitle());
        //    itemViewHolder.text_msg_time.setText(mDatas.get(position).getCreateTime());
        String imgUrl = mDatas.get(position).getImgUrl();
        if (imgUrl != null & !imgUrl.equals("")) {
            ImageUtils.loadImageView(mContext, imgUrl, itemViewHolder.imageView);
        } else {
            ImageUtils.loadImageView(mContext, R.drawable.me_img_default, itemViewHolder.imageView);
        }


        itemViewHolder.article_information.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                type = mDatas.get(position).getType();
                productId = mDatas.get(position).getTargetId();
                if (type.equals("1")) {
                    /*产品*/
                    Intent intent = new Intent(mContext, ProductDetailsActivity.class);
                    intent.putExtra("productId", productId);
                    mContext.startActivity(intent);
                } else if (type.equals("2")) {
                    /*展赛*/
                } else if (type.equals("3")) {
                    /*展览*/
                } else if (type.equals("4")) {
                     /*热点文章*/
                    Intent intent = new Intent();
                    intent.setClass(mContext, CreativeHotspotDetailsActivity.class);
                    intent.putExtra("hotId", productId);
                    mContext.startActivity(intent);

                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }


    public class ItemViewHolder extends RecyclerView.ViewHolder {

        TextView text_msg, text_msg_time;
        ImageView imageView;
        RelativeLayout article_information;

        public ItemViewHolder(View itemView) {
            super(itemView);
            text_msg = (TextView) itemView.findViewById(R.id.text_msg);
            text_msg_time = (TextView) itemView.findViewById(R.id.text_msg_time);
            imageView = (ImageView) itemView.findViewById(R.id.img_msg);
            article_information = (RelativeLayout) itemView.findViewById(R.id.article_information);
            //  initListener(itemView);
        }

        private void initListener(View itemView) {
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(mContext, "poistion " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

}
